<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="author" content="lijinbo" />
    <script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.11.3/jquery.min.js"></script>
    
    <title>047-2</title>
    
    <script type="module" crossorigin src="../../../../static/common/modulepreload-polyfill-3cfb730f.js"></script>
    <script type="module" crossorigin src="../../../../static/common/047-main-81923f03.js"></script>
    <link rel="stylesheet" href="../../../../static/css/047-style-9099aebd.css">
  </head>

  <body>
    <ul id="ulBox"></ul>
    <script>
      // 使用 IntersectionObserver 异步观察目标元素及其祖先元素或顶级文档视窗交叉状态的方法实现懒加载
      function lazyload() {
        const imgs = document.querySelectorAll('img.lazyload')
        const observer = new IntersectionObserver((nodes) => {
          nodes.forEach((v) => {
            if (v.isIntersecting) {
              // 判断是否进入可视区域
              v.target.src = v.target.dataset.src // 赋值加载图片
              observer.unobserve(v.target) // 停止监听已加载的图片
            }
          })
        })
        imgs.forEach((v) => observer.observe(v))
      }
      // 创建图片
      createImg()
      // 启动懒加载
      setTimeout(() => {
        lazyload()
      }, 50)
    </script>
  </body>
</html>
